cmake_minimum_required(VERSION 3.10)
project(monitor_new)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# 设置编译选项
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -pthread")

# 查找所需的包
find_package(Protobuf REQUIRED)
# 使用pkg-config查找glog
find_package(PkgConfig REQUIRED)
pkg_check_modules(GLOG REQUIRED libglog)

# 包含目录
include_directories(
    ${CMAKE_CURRENT_SOURCE_DIR}
    ${CMAKE_CURRENT_SOURCE_DIR}/config
    ${CMAKE_CURRENT_SOURCE_DIR}/config/proto
    ${CMAKE_CURRENT_SOURCE_DIR}/core
    ${CMAKE_CURRENT_SOURCE_DIR}/executor
    ${CMAKE_CURRENT_SOURCE_DIR}/parser
    ${CMAKE_CURRENT_SOURCE_DIR}/resource_manager
    ${PROTOBUF_INCLUDE_DIRS}
    ${GLOG_INCLUDE_DIRS}
    ${CMAKE_CURRENT_BINARY_DIR}/proto_src
)

# 生成proto文件
set(PROTO_DIR ${CMAKE_CURRENT_SOURCE_DIR}/config/proto)
set(PROTO_FILES ${PROTO_DIR}/config.proto ${PROTO_DIR}/system_info.proto)
set(PROTO_SRC_DIR ${CMAKE_CURRENT_BINARY_DIR}/proto_src)
file(MAKE_DIRECTORY ${PROTO_SRC_DIR})

# 创建一个自定义命令来生成proto源文件
add_custom_command(
  OUTPUT ${PROTO_SRC_DIR}/config.pb.cc ${PROTO_SRC_DIR}/config.pb.h 
         ${PROTO_SRC_DIR}/system_info.pb.cc ${PROTO_SRC_DIR}/system_info.pb.h
  COMMAND ${PROTOBUF_PROTOC_EXECUTABLE} 
          --cpp_out=${PROTO_SRC_DIR} 
          --proto_path=${PROTO_DIR}
          --experimental_allow_proto3_optional
          ${PROTO_FILES}
  DEPENDS ${PROTO_FILES}
  COMMENT "Generating protobuf files"
)

# 创建一个库来包含生成的proto文件
add_library(proto_lib
  ${PROTO_SRC_DIR}/config.pb.cc
  ${PROTO_SRC_DIR}/system_info.pb.cc
)

target_link_libraries(proto_lib ${PROTOBUF_LIBRARIES})

# 定义可执行文件的源文件
set(MONITOR_MAIN_SOURCES
    main.cc
    core/monitor_manager.cc
    config/config_manager.cc
    executor/system_tool_executor.cc
    parser/system_tool_parser.cc
    parser/system_file_parser.cc
    executor/system_file_executor.cc
    resource_manager/resource_manager.cc
)

# 创建可执行文件
add_executable(monitor_main ${MONITOR_MAIN_SOURCES})

# 链接库
target_link_libraries(monitor_main 
  proto_lib
  ${PROTOBUF_LIBRARIES}
  ${GLOG_LIBRARIES}
  pthread
)